<template>
  <div>
    <el-form inline="true" :model="flight" ref="flight" :rules="ruleForm" label-width="100px">


      <div class="form-element">
        <div class="col-md-12 padding-0">
          <div class="col-md-12">

            <div class="panel form-element-padding">
              <div class="panel-heading">
                <h4>新建航班</h4>
              </div>


              <div style="padding-left:160px">


                <el-form-item label="飞机型号" prop="size">
                  <el-select v-model="flight.size"  clearable placeholder="飞机型号">
                    <el-option value="3" label="大型机"></el-option>
                    <el-option value="2" label="中型机"></el-option>
                    <el-option value="1" label="小型机"></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="航空公司" prop="airline">
                  <el-select v-model="flight.airline" clearable placeholder="请选择航空公司">
                    <el-option
                        v-for="(item,index) in airCompanies"
                        :key="index"
                        :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="起点" prop="srcCity">
                  <el-select v-model="flight.srcCity" @change="getSrcAirports(flight.srcCity)" clearable
                             placeholder="出发城市">
                    <el-option
                        v-for="(item,index) in cities"
                        :key="index"
                        :label="item"
                        :value="item">
                    </el-option>
                  </el-select>

                </el-form-item>

                <el-form-item label="出发机场" prop="srcAirport">
                  <el-select v-model="flight.srcAirport" clearable placeholder="出发机场">
                    <el-option
                        v-for="(item,index) in srcAirports"
                        :key="index"
                        :label="item"
                        :value="item">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="终点"prop="dstCity">
                  <el-select v-model="flight.dstCity" @change="getDstAirports(flight.dstCity)" clearable
                             placeholder="到达城市">
                    <el-option
                        v-for="(item,index) in cities"
                        :key="index"
                        :label="item"
                        :value="item">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="目的机场" prop="dstAirport">
                  <el-select v-model="flight.dstAirport" clearable placeholder="目的机场">
                    <el-option
                        v-for="(item,index) in dstAirports"
                        :key="index"
                        :label="item"
                        :value="item">
                    </el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="经济舱价格" prop="economicPrice">
                  <el-input
                      placeholder="例如: 100"
                      v-model="flight.economicPrice"
                      clearable>
                  </el-input>
                </el-form-item>
                <el-form-item label="头等舱价格" prop="advancedPrice">
                  <el-input
                      placeholder="例如: 100"
                      v-model="flight.advancedPrice"
                      clearable>
                  </el-input>
                </el-form-item>

                <el-form-item label="总座位数" prop="seatNum">
                  <el-input
                      placeholder="例如: 100"
                      v-model="flight.seatNum"
                      clearable>
                  </el-input>
                </el-form-item>

                <el-form-item label="头等舱数" prop="advancedNum">
                  <el-input
                      placeholder="例如: 20"
                      v-model="flight.advancedNum"
                      clearable>
                  </el-input>
                </el-form-item>

                <el-form-item label="起飞时间" prop="startTime">
                  <el-date-picker
                      value-format="yyyy-MM-dd HH:mm:ss"
                      style="width:200px"
                      type="datetime"
                      v-model="flight.startTime"
                      clearable>
                  </el-date-picker>
                </el-form-item>

                <el-form-item label="到达时间" prop="endTime">
                  <el-date-picker
                      value-format="yyyy-MM-dd HH:mm:ss"
                      style="width:200px"
                      type="datetime"
                      v-model="flight.endTime"
                      clearable>
                  </el-date-picker>
                </el-form-item>

              </div>
            </div>
          </div>
        </div>


      </div>

      <el-button @click="update('flight')" type="primary" plain>更新</el-button>


    </el-form>
  </div>

</template>

<script>
export default {
  name: "UpdateFlight",
  data() {
    return {
      cities: [],
      srcCity: '',
      dstCity: '',
      srcAirports: [],
      dstAirports: [],
      airports: [],
      result: '',
      airCompanies: [{
        value: '东方航空',
      }, {
        value: 'SCAU航空',
      }, {
        value: '南方航空',
      }, {
        value: '东方航空',
      }, {
        value: '邮政航空',
      }, {
        value: '国际航空',
      }, {
        value: '联合航空',
      }, {
        value: '大新华航空 '
      }, {
        value: '飞龙航空 '
      }
      ],
      airCompany: '',
      flight: {
        id:null,
        airline: '',
        srcCity: '',
        dstCity: '',
        srcAirport: '',
        dstAirport: '',
        startTime: '',
        endTime: '',
        advancedPrice: null,
        economicPrice: null,
        seatNum: null,
        seatCount: 0,
        advancedNum: null,
        economicNum:0,
        size: null,
        sizeStr: '中型机',
        advancedCount: 0,
      },
      ruleForm:{
        airline:[{required:true,message:'航空公司不为空',trigger:'blur'}],
        srcCity:[{required:true,message:'起点在哪',trigger:'blur'}],
        srcAirport:[{required:true,message:'起点在哪',trigger:'blur'}],
        dstCity:[{required:true,message:'终点点在哪',trigger:'blur'}],
        dstAirport:[{required:true,message:'终点在哪',trigger:'blur'}],

        economicPrice: [{required:true,message:'价格不能为空',trigger:'blur'}],
        advancedPrice: [{required:true,message:'价格不能为空',trigger:'blur'}],
        seatNum: [{required:true,message:'数目不能为空',trigger:'blur'}],
        advancedNum: [{required:true,message:'数目不能为空',trigger:'blur'}],
        size: [{ required: true, message: '不能没有飞机型号', trigger: 'blur' }],
        startTime:[{required:true,message:'时间必须要有',trigger:'blur'}],
        endTime:[{required:true,message:'时间必须要有',trigger:'blur'}],


      },

    }
  },
  methods: {

    getSrcAirports(city) {
      this.flight.srcAirport=null;
      const _this = this
      this.$axios.post("http://localhost:8181/flight/getAirports", {city: city})
          .then((resp) => {
            _this.srcAirports = resp.data
          })
    },
    getDstAirports(city) {
      this.flight.dstAirport=null;
      const _this = this
      this.$axios.post("http://localhost:8181/flight/getAirports", {city: city})
          .then((resp) => {
            _this.dstAirports = resp.data
          })
    },
    update(formName) {

      if(!this.checkOut()){
        return false;
      }
      console.log(this.flight)
      if(this.flight.economicPrice>0){
        alert("is number")
      }
      else{
        alert("not a num")
      }
      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8181/flight/updateFlight', this.flight)
              .then(function (resp) {
                if (resp.data=='success') {
                  _this.$message.success("航班"+_this.flight.id+'已修改')
                } else {
                  _this.$message.error("错误请重试")
                }
                console.log(_this.flight)
              })
        }

      })

    },
    checkOut() {


      if (this.flight.srcAirport ===null) {
        alert("请选择出发机场");
        return false;
      }
      if (this.flight.dstAirport === null) {
        alert("请选择目的机场");
        return false;
      }

      if (this.flight.size === "飞机型号") {
        alert("请选择飞机类型");
        return false;
      }
      if (this.flight.airline === "航空公司") {
        alert("请选择航空公司");
        return false;
      }
      if (isNaN(this.flight.advancedNum) || isNaN(this.flight.seatNum) || isNaN(this.flight.advancedPrice) || isNaN(this.flight.economicPrice)) {
        alert("请输入数字");
        return false;
      }
      if (parseInt(this.flight.advancedNum) > parseInt(this.flight.seatNum)) {
        alert("头等舱数量不可大于总座位数");
        return false;
      }
      if (parseFloat(this.flight.advancedPrice) < parseFloat(this.flight.economicPrice)) {
        alert("经济舱价格不能高于头等舱");
        return false;
      }
      if (this.flight.srcAirport === this.flight.dstAirport) {
        alert("不能原地盘旋");
        return false;
      }



      if (this.flight.size === 1) {
        if (this.flight.seatNum % 4 != 0 || this.flight.advancedNum % 4 != 0) {
          alert("小型机座位数为4*N");
          return false;
        }
      }
      if (this.flight.size === 2) {
        if ( this.flight.seatNum% 6 != 0 || this.flight.advancedNum % 6 != 0) {
          alert("中型机座位数为6*N");
          return false;
        }
      }
      if (this.flight.size === 3) {
        if (this.flight.seatNum % 8 != 0 || this.flight.advancedNum % 8 != 0) {
          alert("大型机座位数为8*N");
          return false;
        }
      }

      if (this.flight.endTime < this.flight.startTime) {
        alert("到达时间不能小于出发时间");
        return false;
      }

      //计算经济舱个数
      this.flight.economicNum=this.flight.seatNum-this.flight.advancedNum;
      alert("经济舱个数"+this.flight.economicNum);

      return true;

    }

  },
  created() {
    this.flight.id=this.$route.query.id
    const _this = this
    this.$axios.get("http://localhost:8181/flight/loadCity")
        .then((resp) => {
          _this.cities = resp.data
        })
    this.$axios.get('http://localhost:8181/flight/getFlight/'+_this.flight.id)
        .then(function (resp){
          _this.flight=resp.data
        })

  },


}
</script>



<style scoped>

</style>
